package com.qf.service.impl;/*
 *保护你的大熊
 *2021/11/29
 *17:38
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DtsBrandServiceImpl implements DtsBrandService {

    @Autowired
    DtsBrandMapper brandMapper;

    @Override
    public List<Integer> findBreandIds() {

        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getDeleted,false);

        List<DtsBrand> brands = brandMapper.selectList(wrapper);

        List<Integer> ids = brands.stream().map(dtsBrand -> {
            return dtsBrand.getId();
        }).collect(Collectors.toList());

        return ids;
    }

    @Override
    public List<Map<String, Object>> findBrandList() {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getDeleted,false);
        List<DtsBrand> brands = brandMapper.selectList(wrapper);

        List<Map<String,Object>> data = new ArrayList<>();

        for (DtsBrand brand : brands) {
            Map<String,Object> map = new HashMap<>();
            map.put("value",brand.getId());
            map.put("label",brand.getName());

            data.add(map);
        }

        return data;
    }

    @Override
    public boolean isExists(Integer brandId) {

        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getDeleted,false);
        wrapper.eq(DtsBrand::getId,brandId);

        int count = brandMapper.selectCount(wrapper);

        return count != 0;

//        if(count == 0){
//            return false;
//        }
//
//        return true;
    }
}
